//
//  DetialViewController.m
//  BuffetNews
//
//  Created by 李荃荃 on 14-10-27.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DetialViewController.h"
#import "NetHandler.h"
#import "CommentCell.h"
#import "UIImageView+WebCache.h"
#import "CompareDate.h"
#import "WebColor.h"
@interface DetialViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, retain)UITableView *tittleVC;
@property (nonatomic, retain)UILabel *headerLabel;
@end

@implementation DetialViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray array];
    }
    return self;
}
- (void)dealloc
{
    [_arr release];
    [_tittleVC release];
    [_headerLabel release];
    [_pkString release];
    [_tittle release];
    [_modeView release];
    [_hiddeView release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self netHandel];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    label.backgroundColor = [WebColor orangeRed];
    UILabel *tiLb = [[UILabel alloc]initWithFrame:CGRectMake(50, 5, label.frame.size.width, label.frame.size.height)];
    tiLb.font = [UIFont fontWithName:@"Helvetica-Bold"  size:13];
    tiLb.textColor = [UIColor whiteColor];
    tiLb.text = self.tittle;
    [self.view addSubview:label];
    [self.view addSubview:tiLb];
    UIButton *back = [UIButton buttonWithType:UIButtonTypeSystem];
    back.frame = CGRectMake(0, 20, 32, 32);
    [back addTarget:self action:@selector(backTap:) forControlEvents:UIControlEventTouchUpInside];
    [back setImage:[UIImage imageNamed:@"h.png"] forState:UIControlStateNormal];
    [self.view addSubview:back];
    _tittleVC = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height - 50)];
    _tittleVC.dataSource = self;
    _tittleVC.delegate = self;
    [self.view addSubview:_tittleVC];
    [label release];
    [_tittleVC release];
    [tiLb release];
}
- (void)backTap:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)netHandel
{
    NSString *str = @"http://c.myzaker.com/weibo/api_comment_article_url.php?_appid=iphone&_dev=iPhone%2C7.1.2&_v=4.4.4&_version=4.51&act=get_comments&pk=";
    
    NSString *strNet = [str stringByAppendingString:self.pkString];
    NSLog(@"%@",strNet);
    [NetHandler networkWithURL:strNet comaletion:^(id result) {
       self.arr = [[result objectForKey:@"data"]objectForKey:@"comments"];
        if ([[[result objectForKey:@"data"]objectForKey:@"comments"] count] == 0) {
            UILabel *bigView = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height - 50)];
            bigView.backgroundColor = [WebColor floralWhite];
            UILabel *smallView = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 4, self.view.frame.size.height / 2, 200, 20)];
            smallView.text = @"暂无更多评论,请返回";
            smallView.font = [UIFont fontWithName:@"Helvetica-Bold"  size:13];
            smallView.textColor = [UIColor grayColor];
            [self.view addSubview:bigView];
            [self.view addSubview:smallView];
            
            [bigView release];
            [smallView release];
        }else{
        [_tittleVC reloadData];
        }
    }];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[_arr objectAtIndex:section]objectForKey:@"title"];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.arr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[[_arr objectAtIndex:section]objectForKey:@"list"] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"a"];
    if (cell == nil) {
        cell = [[[CommentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"a"] autorelease];
    }
    NSDictionary*dic = [_arr objectAtIndex:indexPath.section];
    cell.nameLabel.text = [[[dic objectForKey:@"list"]objectAtIndex:indexPath.row]objectForKey:@"auther_name"];
    cell.commentLabel.text = [[[dic objectForKey:@"list"]objectAtIndex:indexPath.row]objectForKey:@"content"];
    cell.commentLabel.numberOfLines = 0;
    [cell.headImage sd_setImageWithURL:[[[dic objectForKey:@"list"]objectAtIndex:indexPath.row]objectForKey:@"auther_icon"] placeholderImage:[UIImage imageNamed:@"e.png"]];
    cell.headImage.layer.cornerRadius = 16;
    cell.headImage.clipsToBounds = YES;
    [cell.headImage.layer setBorderWidth:1];
    [cell.headImage.layer setBorderColor:[[UIColor grayColor]CGColor]];
    
    
    [cell.good setTitle:[[[dic objectForKey:@"list"]objectAtIndex:indexPath.row]objectForKey:@"like_num"] forState:UIControlStateNormal];
    cell.good.titleLabel.backgroundColor = [UIColor whiteColor];
    cell.good.titleLabel.font = [UIFont systemFontOfSize:10];
    
    cell.dataLabel.font = [UIFont systemFontOfSize:10];
    
    
    NSString *data = [[[dic objectForKey:@"list"]objectAtIndex:indexPath.row]objectForKey:@"date"];
    NSString *time = [[[dic objectForKey:@"list"]objectAtIndex:indexPath.row]objectForKey:@"time"];
    NSString *allTime = [NSString string];
    allTime = [data stringByAppendingString:@" "];
    
    NSString *sumTime = [allTime stringByAppendingString:time];
    
    NSString *lastTime = [sumTime stringByAppendingString:@" +0800"];
    
    
    NSDateFormatter *format = [[NSDateFormatter alloc]init];
    
    
    NSLocale *enLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en-US"];
    [format setLocale:enLocale];
    
    [format setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"];
    NSDate *dateTime = [format dateFromString:lastTime];
    
    
    NSString *strTime = [CompareDate compareCurrentTime:dateTime];
    cell.dataLabel.text = strTime;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.bad addTarget:self action:@selector(badTap:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    
    [enLocale release];
    [format release];
}
- (void)badTap:(UIButton *)button
{
        _modeView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 32)];
        _modeView.backgroundColor = [UIColor blackColor];
        _modeView.alpha = 0.5;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCelnce:)];
    [_modeView addGestureRecognizer:tap];
        [self.view addSubview:_modeView];
    [tap release];
        
        
        _hiddeView = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height , self.view.frame.size.width, 80)];
        _hiddeView.backgroundColor = [UIColor whiteColor];
        
        [self.view addSubview:_hiddeView];
        
        [UIView animateWithDuration:0.3 animations:^{
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
            _hiddeView.frame = CGRectMake(0, self.view.frame.size.height- 80, self.view.frame.size.width, 80);
            UIButton *badButton = [UIButton buttonWithType:UIButtonTypeSystem];
            badButton.frame = CGRectMake(_hiddeView.frame.size.width / 2 - 20,20, 32, 32);
            [badButton setImage:[[UIImage imageNamed:@"g.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            [badButton.layer setBorderColor: [[UIColor grayColor]CGColor]];
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(_hiddeView.frame.size.width / 2 - 18,52, 34, 34)];
            label.text = @"举报";
            label.font = [UIFont fontWithName:@"Helvetica-Bold"  size:13];
            [badButton addTarget:self action:@selector(badButtonTap:) forControlEvents:UIControlEventTouchUpInside];
            [_hiddeView addSubview:label];
            [_hiddeView addSubview:badButton];
            [label release];
        }];
        [UIView animateWithDuration:0.6 animations:^{
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
            _modeView.backgroundColor = [UIColor blackColor];
            _modeView.alpha = 0.5;
        }];
    [_modeView release];
    [_hiddeView release];
}
- (void)tapCelnce:(UITapGestureRecognizer *)tap
{
    [_hiddeView removeFromSuperview];
    [_modeView removeFromSuperview];
}
- (void)doTimeStop
{
    [_headerLabel removeFromSuperview];
}
- (void)badButtonTap:(UIButton *)button
{
    [_hiddeView removeFromSuperview];
    [_modeView removeFromSuperview];
    _headerLabel = [[UILabel alloc]init];
    _headerLabel.frame = CGRectMake(0, 0, self.view.frame.size.width, 50);

    _headerLabel.text =@"举报成功";
    _headerLabel.font = [UIFont boldSystemFontOfSize:14];
    _headerLabel.textAlignment = NSTextAlignmentCenter;
    _headerLabel.backgroundColor = [WebColor forestGreen];
    _headerLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:_headerLabel];
    
    NSTimer *timer1 = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(doTimeStop) userInfo:nil repeats:NO];
    NSLog(@"%@", timer1);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = @{NSFontAttributeName :[UIFont systemFontOfSize:17]};
    CGRect rect =[[[[[_arr objectAtIndex:indexPath.section]objectForKey:@"list"]objectAtIndex:indexPath.row]objectForKey:@"content"] boundingRectWithSize:CGSizeMake(self.view.frame.size.width * 4 / 5 - 70, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];;
    return rect.size.height + 80;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSString *stringTittle = [self tableView:tableView titleForHeaderInSection:section];
    if (stringTittle == nil) {
        return nil;
    }
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 70, 20)];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [WebColor darkGray];
    label.text = stringTittle;
    label.font = [UIFont systemFontOfSize:10];
    UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)] autorelease];
    [sectionView setBackgroundColor:[UIColor clearColor]];
    [sectionView addSubview:label];
    return sectionView;

    [sectionView release];
    [label release];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
